﻿''' <summary>
''' 手动数据上传操作界面
''' </summary>
''' <remarks></remarks>
Public Class FrmDeviceRestartTime
    Private bstatus As Boolean = False
    Private projectID As String


    Private Sub FrmDeviceRestartTime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        projectID = MainForm.pi.ProjectID
        LoadDevices()
    End Sub


    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If (Not bstatus) Then
            If (chkDevicesList.CheckedItems.Count < 1) Then
                MessageBox.Show("未选择要上传的设备")
                Return
            End If

            bstatus = True
            Me.pbUpload.Visible = True
            Dim lst As IList(Of DevTypeItem) = New List(Of DevTypeItem)
            For index As Integer = 0 To Me.chkDevicesList.CheckedItems.Count - 1
                Dim dev As DevTypeItem
                dev = CType(CType(Me.chkDevicesList.CheckedItems(index), FormatObject).Obj2, DevTypeItem)

                lst.Add(dev)
            Next

            bkWorker.RunWorkerAsync(lst)
            btnUpload.Text = "取消"
        Else
            '' bstatus = False
            bkWorker.CancelAsync()
            ''    btnUpload.Text = "上传"
        End If

    End Sub

    Private Sub LoadDevices()
        Dim service As New DeviceRestartTimeService
        Dim lst As IList(Of DevTypeItem) = service.GeDevices(projectID)

        Me.chkDevicesList.Items.Clear()

        For i As Integer = 0 To lst.Count - 1
            Me.chkDevicesList.Items.Add(New FormatObject("{0}#设备", lst(i).NetId, lst(i)))
        Next
    End Sub

    Friend Class FormatObject

        Private fmt As String
        Public Obj1 As Object
        Public Obj2 As Object

        Sub New(ByVal format As String, ByVal obj1 As Object, ByVal obj2 As Object)
            fmt = format
            Me.Obj1 = obj1
            Me.Obj2 = obj2
        End Sub

        Public Overrides Function ToString() As String
            Return String.Format(fmt, Obj1)
        End Function
    End Class

    Private Sub bkWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkWorker.DoWork
        Dim lst As IList(Of DevTypeItem) = New List(Of DevTypeItem)
        If (e.Argument IsNot Nothing) Then
            lst = CType(e.Argument, IList(Of DevTypeItem))

            UploadData(lst)

        End If
        bkWorker.ReportProgress(100)
    End Sub

    ''' <summary>
    ''' 上传数据
    ''' </summary>
    ''' <param name="lst"></param>
    ''' <remarks></remarks>
    Private Sub UploadData(ByVal lst As IList(Of DevTypeItem))
        If (bkWorker.CancellationPending) Then
            Return
        End If

        Dim iprocess As Integer = 5
        bkWorker.ReportProgress(iprocess)
        Dim icout As Integer = lst.Count
        Dim ipercent As Integer = 90 / icout
        Dim dev As DevTypeItem
        Dim ilst As IList(Of DeviceRestartTime)
        Dim service As New DeviceRestartTimeService()

        For index As Integer = 0 To lst.Count - 1
            If (bkWorker.CancellationPending) Then
                Return
            End If

            dev = lst(index)
            ilst = DevicePowerRestartTime.GetPowerTimeList(dev.NetId)

            iprocess += ipercent / 2
            bkWorker.ReportProgress(iprocess)
            Dim dt As DateTime = service.GetDeviceLastStartTime(dev.NetId, projectID)
            Dim inlst As IList(Of DeviceRestartTime) = New List(Of DeviceRestartTime)()
            Dim dtfrist As DateTime = service.GetDeviceLastFirstTime(dev.NetId, projectID)


            dtfrist = GetFirstTime(ilst, dtfrist)

            System.Diagnostics.Debug.WriteLine(String.Format("dtFirst:{0} ", dtfrist.ToString("yyyy-MM-dd HH:mm:ss")))

            If (bkWorker.CancellationPending) Then
                Return
            End If

            For j As Integer = 0 To ilst.Count - 1
                If (DateTime.Compare(ilst(j).RestartedTime, dt) > 0) Then
                    ilst(j).FirstTime = dtfrist
                    inlst.Add(ilst(j))
                End If
            Next

            If (inlst.Count > 0) Then
                service.AddRecords(inlst, projectID)
            End If

            iprocess += ipercent / 2
            bkWorker.ReportProgress(iprocess)
        Next

        iprocess = 95
        bkWorker.ReportProgress(iprocess)
    End Sub

    ''' <summary>
    ''' 比较队列中的FirstTime
    ''' </summary>
    ''' <param name="lst">要检查的队列</param>
    ''' <param name="dtFirst">用来比较的值</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetFirstTime(ByVal lst As IList(Of DeviceRestartTime), ByVal dtFirst As DateTime) As DateTime

        If (lst.Count = 0) Then
            Return dtFirst
        End If
        Dim dtResult As DateTime = lst(0).RestartedTime
        For i As Integer = 0 To lst.Count - 1

            System.Diagnostics.Debug.WriteLine(String.Format("DTRestart:{0} dtResult:{1}", lst(i).RestartedTime.ToString("yyyy-MM-dd HH:mm:ss"), dtResult.ToString("yyyy-MM-dd HH:mm:ss")))

            If (DateTime.Compare(lst(i).RestartedTime, dtResult) < 0) Then
                dtResult = lst(i).RestartedTime
            End If
        Next

        If (DateTime.Compare(dtResult, dtFirst) = 0) Then
            Return dtFirst
        Else
            Return dtResult
        End If
    End Function

    Private Sub bkWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bkWorker.ProgressChanged
        Me.pbUpload.Value = e.ProgressPercentage
        Me.pbUpload.Invalidate()
    End Sub

    Private Sub bkWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bkWorker.RunWorkerCompleted


        bstatus = False
        btnUpload.Text = "上传"


        If (e.Cancelled = False) Then
            If (e.Error IsNot Nothing) Then
                MessageBox.Show("上传数据失败")
            Else
                MessageBox.Show("上传数据完毕")
            End If
        End If
        Me.pbUpload.Visible = False
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        If (bkWorker.IsBusy) Then
            bkWorker.CancelAsync()
        End If

        Me.Close()
    End Sub
End Class